'''
coding:utf-8
@Software:PyCharm
@Time:2022/6/9 18:15
@Author:liuyi
'''

class User:
    def __init__(self, number, name):
        self.number = number
        self.name = name
        self.next = None

    def __str__(self):
        return "编号：{0}\t姓名：{1}".format(self.number, self.name)

    def getNumber(self) -> int:
        return self.number


class LinkedList:
    def __init__(self):
        self.head = User(0, "")

    # 添加
    def addList(self, User):
        temp = self.head
        while True:
            if temp.next == None:
                temp.next = User
                break
            temp = temp.next
        temp.next = User

    # 根据编号插入到指定位置
    def NewAddList(self, User):
        temp = self.head
        boolean = False
        while True:
            if temp.next == None:
                break
            if temp.next.getNumber() > User.getNumber():
                break
            if temp.next.getNumber() == User.getNumber():
                boolean = True
                break
            temp = temp.next
        if boolean:
            print(f"{User.getNumber()}已经存在，不能添加")
        else:
            User.next = temp.next
            temp.next = User

    # 打印
    def showList(self):
        if self.head.next == None:
            print("链表为空~")
            return
        temp = self.head.next
        while True:
            print(temp)
            if temp.next == None:
                break
            temp = temp.next

    # 反转
    def reversalList(self):
        if self.head.next == None:
            return
        temp = self.head.next
        user_text = User(0, "")
        count_temp = None
        while temp != None:
            count_temp = temp.next
            temp.next = user_text.next
            user_text.next = temp
            temp = count_temp
        self.head.next = user_text.next


if __name__ == '__main__':
    user1 = User(1, "zhangsan1")
    user2 = User(2, "zhangsan2")
    user3 = User(3, "zhangsan3")

    lisked_list = LinkedList()
    lisked_list.addList(user1)
    lisked_list.addList(user2)
    lisked_list.addList(user3)
    lisked_list.NewAddList(user3)

    lisked_list.reversalList()
    lisked_list.showList()

